package com.pf.business.export.controller;

import com.alibaba.excel.EasyExcel;
import com.pf.business.export.dto.ExportFillBill;
import com.pf.business.export.dto.ExportFillCriteria;
import com.pf.business.export.handler.BillCellWriteHandler;
import com.pf.business.export.handler.CriteriaCellWriteHandler;
import com.pf.business.export.helper.ExportDataHelper;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;

/**
 * easyExcel 填充导出
 *
 * @author PanFei
 * @version 1.0.0
 * @createTime 2023/7/15 19:02
 */
@RestController
@RequestMapping("/excel")
public class FillExportController {

    /**
     * 账单填充导出
     * <p>
     * excel有超链接，样式修改，有界下拉列表
     *
     * @param response
     * @param nums
     * @throws Exception
     */
    @GetMapping("/billFillExport")
    public void billFillExport(HttpServletResponse response, @RequestParam(value = "nums", defaultValue = "10") Integer nums) throws Exception {
        List<ExportFillBill> billList = ExportDataHelper.getFillBillData(nums);
        ClassPathResource classPathResource = new ClassPathResource("templates/bill_fill_template.xlsm");
        String fileName = "账单_" + System.currentTimeMillis();
        fileName = URLEncoder.encode(fileName, "UTF-8") + ".xlsm";
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
        EasyExcel.write(response.getOutputStream())
                .withTemplate(classPathResource.getInputStream())
                .registerWriteHandler(new BillCellWriteHandler())
                .sheet()
                .doFill(billList);
    }

    /**
     * 准则填充导出
     * <p>
     * excel有超链接，样式修改，无限的下拉选项列表，下拉级联
     *
     * @param response
     * @throws Exception
     */
    @GetMapping("/criteriaFillExport")
    public void criteriaFillExport(HttpServletResponse response) throws Exception {
        List<ExportFillCriteria> criteriaList = ExportDataHelper.getFillCriteriaList();
        ClassPathResource classPathResource = new ClassPathResource("templates/criteria_fill_template.xlsm");
        String fileName = "准则_" + System.currentTimeMillis();
        fileName = URLEncoder.encode(fileName, "UTF-8") + ".xlsm";
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
        String sheetName = "准则";
        EasyExcel.write(response.getOutputStream())
                .withTemplate(classPathResource.getInputStream())
                .registerWriteHandler(new CriteriaCellWriteHandler(sheetName, 1))
                .sheet(sheetName)
                .doFill(criteriaList);
    }
}
